package com.guigu.www.denghao.controller;

import com.guigu.www.denghao.service.DHHealthManagerService;
import com.guigu.www.pojo.*;
import com.guigu.www.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/DHHealthManagerController")
public class DHHealthManagerController {

    @Autowired
    private DHHealthManagerService healthManagerService;

    // 用户管理列表分页查询
    @PostMapping("/findLikeUser")
    public PageVo<HealthManager> findLikeUser(Integer pageNum,Integer pageSize,String hmName,String hmPhone,String hmAge){
        return healthManagerService.findLikeUser(pageNum,pageSize,hmName,hmAge,hmPhone);
    }

    // 咨询师列表展示设置分页查询
    @PostMapping("/findLikeUser1")
    public PageVo<HealthManager> findLikeUser1(Integer pageNum,Integer pageSize,String hmName,String hmPhone,String hmDisplayState){
        return healthManagerService.findLikeUser1(pageNum,pageSize,hmName,hmDisplayState,hmPhone);
    }

    @PostMapping("/addUser")
    public Integer addUser(@RequestBody HealthManager healthManager){
        return healthManagerService.addUser(healthManager);
    }

    @PutMapping("/upUser")
    public Integer upUser(@RequestBody HealthManager healthManager){
        return healthManagerService.upUser(healthManager);
    }

    @PostMapping("/delUser")
    public Integer delUser(Integer hmId,Integer hmState){
        return healthManagerService.delUser(hmId,hmState);
    }

    @PostMapping("/delDisplayUser")
    public Integer delDisplayUser(Integer hmId,Integer hmDisplayState){
        return healthManagerService.delDisplayUser(hmId,hmDisplayState);
    }

    @PostMapping("/queryByIdHealth")
    public HealthManager queryByIdHealth(Integer hmId){
        return healthManagerService.queryByIdHealth(hmId);
    }

    @PostMapping("/fileUploadTou")
    public Boolean fileUploadTou(@RequestParam("file") MultipartFile file,Integer hmId) throws IOException {
        if(file.isEmpty()){
            return false;
        }else{
            // 获取上传的文件流
            InputStream inputStream = file.getInputStream();

            //String parentPath = "F:\\s3GraduateProject\\physical_project\\src\\assets\\touxiang\\";//获取文件原名
            String originalFilename = file.getOriginalFilename();
            //System.out.println(originalFilename);
            //获取源文件前缀
            String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            //获取源文件后缀
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 将源文件前缀之后加上时间戳避免重名
            String newFileNamePrefix = fileNamePrefix + new Date().getTime();
            //得到上传后新文件的文件名
            String newFileName = newFileNamePrefix+fileNameSuffix;
            boolean b = FtpUtil.uploadFile(newFileName, inputStream);
            //创建一个新的File对象用于存放上传的文件
            //File fileNew = new File(parentPath + newFileName);
            /*try {
                //file.transferTo(fileNew);
            } catch (IOException e) {
                e.printStackTrace();
            }*/
            //System.out.println(hmId);
            healthManagerService.upTouXiang(newFileName,hmId);
        }
        return true;
    }


    @PostMapping("/referenceUserInfo")
    public HealthManager referenceUserInfo(Integer hmId){
        return healthManagerService.referenceUserInfo(hmId);
    }

    @PostMapping("/queryDisplayOrder")
    public List<HealthManager> queryDisplayOrder(){
        return healthManagerService.queryDisplayOrder();
    }

    @PostMapping("/upDisplayOrder")
    public Integer upDisplayOrder(Integer[] hmIds){
        healthManagerService.upDisplayOrder(hmIds);
        return 0;
    }

    @PostMapping("/queryByMemIdConstitutionTest")
    public ConstitutionTest queryByMemIdConstitutionTest(Integer memId){
        return healthManagerService.queryByMemIdConstitutionTest(memId);
    }

    @PostMapping("/queryByMemIdMindTest")
    public MindTest queryByMemIdMindTest(Integer memId){
        return healthManagerService.queryByMemIdMindTest(memId);
    }

    @PostMapping("/queryByMemIdRiskTest")
    public RiskTest queryByMemIdRiskTest(Integer memId){
        return healthManagerService.queryByMemIdRiskTest(memId);
    }


}
